<template>
  <div>
    <!-- 需求计划模板下发 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhTemplateDistribution"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onSyncTableData="syncTableFun"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" @click="addHandle(1)">数据导入</el-button>
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 2)" :disabled="isIssue">下发</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="'导入'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <importFile :importData="formData" @close="emitcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { vehicleConfiguration } from '@/api/omFinance';
import { postList, postdone, delList } from '@/api/omSale/salesPlan/TemplateDistribution';
import importFile from './importFile';

export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: postList,
      fetchParams: {
        templateYearAndMonth: moment().format('YYYY-MM'),
        isDealer: 10041002
      },
      formData: {},
      visible: false,
      isIssue: true,
      selectedList: []
    };
  },
  mixins: [dictionary],
  components: {
    importFile
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getvehicleList();
  },
  methods: {
    syncTableFun(data) {
      if (data.length > 0) {
        let disabledSelect = data.filter(row => row.templateStatus===60231002)
        this.$refs.table.$refs.pageTable.SET_DISABLE_SELECT(disabledSelect)
      }
      
    },
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    selectChangeHandle(row) {

      if (row.length > 0) {

        this.selectedList = row;
        this.isIssue = false;
        return;

      }

      this.isIssue = true;
    },
    async addHandle(type) {
      if (type == 1) {
        this.visible = true;
        this.formData.name = '需求计划模板下发';
        this.formData.upUrl = 'https://bjev-cms-oss-public.oss-cn-beijing.aliyuncs.com/需求计划模板下发.xlsx';
      } else {
        let arr = [];
        this.selectedList.map(x => {
          arr.push(x.id);
        });
        await this.$confirm('是否下发?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let res = await postdone({ templateIds: arr });
            if (res.resultCode == 200) {
              this.$notify.success({
                title: '提示信息',
                message: '下发成功'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
              this.isIssue = true;
            }
          })
          .catch(() => {});
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
      this.isIssue = true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    emitcloseHandler() {
      this.visible = false;
      this.isIssue = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    closeHandler(val) {
      console.log(val);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '请输入'
        },

        {
          type: 'DATE',
          label: '月份',
          fieldName: 'templateYearAndMonth',
          initialValue: moment().format('YYYY-MM'),
          dateType: 'month'
        },
        {
          type: 'SELECT',
          label: '下发状态',
          fieldName: 'templateStatus',
          placeholder: '请选择',
          itemList: this.createDictList('6023')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '下发状态',
          dataIndex: 'templateStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6023'),
          render: props => {
            return <span>{this.createDictText(props.row.templateStatus, '6023')}</span>;
          }
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimColorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'N+1月是否可提报',
          dataIndex: 'isAddOne',
          sorter: true,
          filter: true,
          filterType: 'input',
         render: props => {
            return <span>{this.createDictText(props.row.isAddOne, '1004')}</span>;
          }
        },
        {
          title: 'N+2月是否可提报',
          dataIndex: 'isAddTwo',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.isAddTwo, '1004')}</span>;
          }
        },
        {
          title: 'N+3月是否可提报',
          dataIndex: 'isAddThree',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.isAddThree, '1004')}</span>;
          }
        },
        {
          title: '月份',
          dataIndex: 'month',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{props.row.templateYear && props.row.templateMonth ? props.row.templateYear + '年' + props.row.templateMonth + '月' : ''}</span>;
          }
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '创建人',
          dataIndex: 'createdByName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '下发日期',
          dataIndex: 'assignDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '下发人',
          dataIndex: 'assignPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
